package com.jinsi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinsi.pojo.ScStudentBonus;
import com.jinsi.pojo.ScStudentOperationLog;
import com.jinsi.service.impl.StuOperationLogServiceImpl;
import com.jinsi.service.impl.StudentBonusServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StuBonusController {

    @Autowired
    private StudentBonusServiceImpl server;
    @Autowired
    private StuOperationLogServiceImpl logServer;

    @ResponseBody
    @RequestMapping("/getBonus")
    public Map<String,Object> getBonus(Integer page, Integer limit){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);
        List<ScStudentBonus> list = server.getBonus();
        PageInfo<ScStudentBonus> pageInfo = new PageInfo<>(list);
        map.put("total",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        map.put("state",0);
        map.put("msg","成功");
        return map;
    }

    @ResponseBody
    @RequestMapping("/addBonus")
    public Map<String,String> addBonus(ScStudentBonus bonus){

        Map<String, String> map = new HashMap<>();
        try {
            bonus.setGrantDateTime(new Date());
            server.addBonus(bonus);
            ScStudentOperationLog log = new ScStudentOperationLog();
            log.setStudentId(bonus.getStudentId());
            log.setStudentName(bonus.getStudentName());
            log.setOperationDateTime(new Date());
            log.setOperationRemark("为学生发放奖金");
            log.setStatus(2);
            logServer.addLog(log);
            map.put("state","200");

        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","400");
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/delBonus")
    public Map<String ,String> delBonus(Integer id,String studentId,String studentName){

        Map<String, String> map = new HashMap<>();
        try {
            server.delBonus(id);
            ScStudentOperationLog log = new ScStudentOperationLog();
            log.setStudentId(studentId);
            log.setStudentName(studentName);
            log.setOperationDateTime(new Date());
            log.setOperationRemark("删除学生奖学金信息");
            log.setStatus(2);
            logServer.addLog(log);
            map.put("state","200");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","400");
        }
        return map;
    }



}
